<?php
    /* functions */
    function serviceBgate($data)
    {
        $url = "http://113.52.37.66/HDCConnect/dosth?pw=hdcbgatesms";
        $url .= "&p=" . urlencode($data["sender"]);
        $url .= "&id=" . urlencode($data["transactionId"]);
        $url .= "&serviceCode=" . urlencode($data["serviceCode"]);
        $url .= "&f=" . urlencode($data["content"]);
        $url .= "&m=" . urlencode(checkTelcoBgate($data["sender"]));
        
        $response = cUrl($url);
        $response = trim($response);
                
        $result = array();
        if($response=="Servlet dosth at wrong password"){
            $result["status"] = 0;    
            $result["message"] = "Sai mat khau truy cap api";    
        } else {
            $response = explode("|", $response);
            $response = isset($response[3]) ? $response[3] : "";
            if(preg_match("/Giao dich thanh cong/si", $response)){
                $result["status"] = 1;    
                $result["message"] = "Giao dich thanh cong";    
            } else if(preg_match("/Giao dich khong thanh cong/si", $response)){
                $result["status"] = 0;    
                $result["message"] = "Giao dich khong thanh cong";    
            } else {
                $result["status"] = 0;    
                $result["message"] = "Invalid Request API";    
            }    
        }
        
        return $result;
    }
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $game_name = !empty($match[2]) ? $match[2] : "";
        $refCode = !empty($match[5]) ? $match[5] : "";
        
        
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["game_name"] = $game_name;
        
        return $result;
    }
    
    function checkTelcoBgate($mobile)
    {        
        $first = substr($mobile, 0, 1);
        if($first=="0"){
            $mobile = substr($mobile, 1, strlen($mobile) - 1);
        } else if($first="8"){
            $mobile = substr($mobile, 2, strlen($mobile) - 2);
        }
        
        $mobile = "84" . $mobile;
        
        $patternViettel = "/^84(9[678]|16[2-9])[\d]{7}/";
        $patternMobi = "/^84(9[03]|12[01268])[\d]{7}/";
        $patternVina = "/^84(9[14]|12[34579])[\d]{7}/";
        $patternVnmobile = "/^84(9[2]|18[86])[\d]{7}/";
        $patternBeeline = "/^84(9[9]|19[9])[\d]{7}/";
        
        if(preg_match($patternViettel, $mobile)){
            $telco = "VIETTEL";    
        } else if(preg_match($patternMobi, $mobile)){
            $telco = "MOBI";
        } else if(preg_match($patternVina, $mobile)){
            $telco = "VINA";
        } else if(preg_match($patternVnmobile, $mobile)){
            $telco = "VNMOBILE";
        } else if(preg_match($patternBeeline, $mobile)){
            $telco = "BEELINE";
        } else {
            $telco = "OTHER";
        }
        
        return $telco;    
    }
      
    /*
    * processing
    */
    
    function processing($data)
    {        
        $regx = "/^TVBG(\s+)([a-zA-Z0-9]{1,})(.*)(\s+)([a-zA-Z0-9_]{1,})$/si";        
        
        if(preg_match($regx, $data["content"])){
            
            $syntax = buildSyntax($data["content"], $regx);
            
            $data["userId"] = $syntax["userId"];
            
            switch ($syntax["game_name"]){
                case "TBG":
                    $data["id"] = 171;
                    break;
                case "BOITV":
                    $data["id"] = 166;
                    break;
                case "NTDM":
                    $data["id"] = 167;
                    break;
                case "HAPPYFARM":
                    $data["id"] = 169;
                    break;
                case "THAPTUCHINH":
                    $data["id"] = 170;
                    break;
                case "GCCC":
                    $data["id"] = 168;
                    break;
                case "CONTRAMAFIA":
                    $data["id"] = 0;
                    break;
                default:
                    $data["id"] = 0;
                    break;
            }

            $list_service_code = array("6522", "6622", "6722");
            if(in_array($data["serviceCode"], $list_service_code)){
                
                $result = serviceBgate($data);                
                $data["status"] = $result["status"];
                
                $response["status"] = $result["status"];
                $response["msg"] = $result["message"];
                
            } else {
                
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;                 
            }               
            
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
            
            $data["id"] = 0; //ID game
        }
        
        /* log sms */        
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 58; //ID partner Bgate    
        $data["type"] = 1; //Thể loại game   
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        $data["action"] = "reg"; //action
        
        $result = InsertSmsLog($data);        
        return $response;
    }
?>